package com.ksc.storekeeper.client.view.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtplatform.mvp.client.ViewImpl;
import com.ksc.storekeeper.client.handlers.PlaceChangeHandler;
import com.ksc.storekeeper.client.i18n.ErrorMessages;
import com.ksc.storekeeper.client.view.StoreAddItemsView;
import com.ksc.storekeeper.shared.dto.ProductDTO;
import com.ksc.storekeeper.shared.enums.Category;

public class StoreAddItemsViewImpl extends ViewImpl implements StoreAddItemsView {

	private final StoreAddItemsViewImplUiBinder uiBinder = GWT.create(StoreAddItemsViewImplUiBinder.class);
	private final ErrorMessages errMsgs = GWT.create(ErrorMessages.class);

	interface StoreAddItemsViewImplUiBinder extends UiBinder<Widget, StoreAddItemsViewImpl> {
	}
	
	@UiField
	TextBox productName;
	
	@UiField
	TextBox productCode;
	
	@UiField
	ListBox productCategory;
	
	@UiField
	TextArea productDescription;
	
	@UiField
	FileUpload productImageUpload;
	
	@UiField
	Button productSubmitBtn;
	
	@UiField
	FormPanel formPanel;
	
	@UiField
	Label status; 
	
	HTMLPanel panel;
	
	private final Widget widget;
	
	private PlaceChangeHandler placeChangeHandler;
	
	private StoreAddItemsUiHandler storeAddItemsUiHandler;

	public StoreAddItemsViewImpl() {
		widget = uiBinder.createAndBindUi(this);
		for(Category category:Category.values())
			productCategory.addItem(category.getValue(),category.name());
		formPanel.setAction("/storekeeper/upload");
		formPanel.setEncoding("multipart/form-data");
		formPanel.setMethod("POST");
		formPanel.addSubmitCompleteHandler(formPanelSubmitCompleteHandler);
		formPanel.addSubmitHandler(formPanelSubmitHandler);		
	}
	
	SubmitHandler formPanelSubmitHandler = new SubmitHandler(){
		@Override
		public void onSubmit(SubmitEvent event) {
		//validation	
		}
	};
	
	SubmitCompleteHandler formPanelSubmitCompleteHandler = new SubmitCompleteHandler(){
		@Override
		public void onSubmitComplete(SubmitCompleteEvent event) {				
			panel = new HTMLPanel(event.getResults());
			if(panel.getElementById("error") == null){				
				storeAddItemsUiHandler.saveProduct(getProductDTO(panel.getElementById("success").getInnerText()));				
				
			}
			else{
				String errorMessage = panel.getElementById("error").getInnerText();
				Window.alert(errMsgs.productSaveFailed(productName.getText(), productCode.getText(), errorMessage));
			}
		}
	};
	
		
	private ProductDTO getProductDTO(String imageUrl){
		ProductDTO productDTO = new ProductDTO();
		productDTO.setName(productName.getText());
		productDTO.setCode(productCode.getText());
		productDTO.setCategory(Category.values()[productCategory.getSelectedIndex()]);
		productDTO.setImageUrl(imageUrl);
		productDTO.setDescription(productDescription.getText());
		return productDTO;
	};
	
	@UiHandler("productSubmitBtn")
	public void formSubmitClick(ClickEvent event){
		formPanel.submit();
	}
	
		@Override
	public Widget asWidget() {
		return this.widget;
	}
	
	public void uploadProductImage(){
	}

	@Override
	public void setPlaceChangeHandler(PlaceChangeHandler placeChangeHandler) {
		this.placeChangeHandler=placeChangeHandler;
	}

	@Override
	public void setStatus(String value) {
		status.setText(value);
	}
	
	@Override
	public void reset(){
		productName.setText("");
		productCode.setText("");
		productDescription.setText("");
		productImageUpload = new FileUpload();
		productCategory.setSelectedIndex(0);
	}

	@Override
	public void setUiHandlers(StoreAddItemsUiHandler uiHandlers) {
		this.storeAddItemsUiHandler = uiHandlers;		
	}

}
